<template>
  <el-dialog :visible.sync="dialogVisible" top="10" width="100%">
    <div class="filter-container">
      <el-input v-model="listQuery.keyword" placeholder="微信昵称 | 买家名称 | 联系手机" style="width: 300px;" class="filter-item" @input="handleSearch" />
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="ID" prop="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="微信">
        <template slot-scope="{row}">
          <div class="avatar">
            <el-avatar :size="50" :src="row.profile.avatar_url" />
            <span>{{ row.profile.nickname }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="认证">
        <template slot-scope="{row}">
          <div v-if="row.enrollment && row.enrollment.state==='ACCEPTED'">
            <div class="avatar">
              <el-avatar v-if="row.enrollment.store_images.length > 0" :size="50" :src="row.enrollment.store_images[0].url" />
              <span>{{ row.enrollment.name }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="选择" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" @click="select(row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </el-dialog>
</template>

<script>
import Pagination from '@/components/Pagination/index'
import { fetchList } from '@/api/user'

export default {
  name: 'UserPicker',

  props: [],

  components:{ Pagination },

  data() {
    return {
      dialogVisible: false,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: ''
      }
    }
  },

  methods: {
    showSelectDialog: async function(){
      this.list = []
      this.total = 0
      this.getList()
      this.dialogVisible = true
    },

    getList(){
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.users
        this.total = response.total
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.total = 0
        this.listLoading = false
        Promise.reject(error)
      })
    },

    handleSearch(){
      this.list = []
      this.listQuery.page = 1
      this.getList()
    },

    select(user){
      this.dialogVisible = false
      this.$emit('onSelectUserConfirm',user)
    }
  }
}
</script>

<style scoped>
.avatar{
  display: flex;
  align-items: center;
}
.avatar span{
  margin-left:10px;
}
</style>
